package com.sskj.hhhjys.ui.activity;


import android.support.design.widget.BottomSheetDialog;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.github.yoojia.inputs.ValueScheme;
import com.github.yoojia.inputs.WidgetAccess;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.hhhjys.R;
import com.sskj.hhhjys.bean.LoginBean;
import com.sskj.hhhjys.bean.rxbus.AccountBean;
import com.sskj.hhhjys.component.DaggerUserDataComponent;
import com.sskj.hhhjys.presenter.LoginActivityPresenter;
import com.sskj.hhhjys.util.AppTipUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.SchemeUtil;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

@Route(path = RConfig.APP_LOGIN)//登录
public class LoginActivity extends BaseActivity<LoginActivityPresenter> {
    @BindView(R.id.tvRegister)
    TextView tvRegister;
    @BindView(R.id.etAccount)
    EditText etAccount;
    @BindView(R.id.ivAccountClear)
    ImageView ivAccountClear;
    @BindView(R.id.etPwd)
    EditText etPwd;
    @BindView(R.id.ivPwdToggle)
    ImageView ivPwdToggle;
    @BindView(R.id.btLogin)
    Button btLogin;
    @BindView(R.id.tvPwdForget)
    TextView tvPwdForget;

    private AndroidNextInputs inputs;
    private boolean isSee;
    @Inject
    UserViewModel userViewModel;
    private BottomSheetDialog googleCheckDialog;
    @BindView(R.id.llPwd)
    LinearLayout llPwd;
    private boolean isPwd = true;
    private Disposable timeDis;
    private WidgetAccess access;
    private AndroidNextInputs inputsCheckCode;

    @Override
    public boolean getImmersion() {
        return false;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.app_activity_login;
    }

    @Override
    public LoginActivityPresenter getPresenter() {
        return new LoginActivityPresenter();
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);
        RxBus.getDefault().register(this);
        ClickUtil.click(tvRegister, () -> {
            ARouter.getInstance().build(RConfig.APP_REGISTER).navigation();
        });


        ClickUtil.click(btLogin, () -> {

            inputs.clear();
            inputs
                    .add(access.findEditText(R.id.etAccount))
                    .with(StaticScheme.Required(), ValueScheme.fixedLength(8))
                    .add(access.findEditText(R.id.etPwd))
                    .with(StaticScheme.NotBlank(), SchemeUtil.getPwdScheme());


            if (inputs.test()) {
                mPresenter.login(etAccount.getText().toString(), etPwd.getText().toString());
            }
        });
        ClickUtil.click(ivAccountClear, () -> {
            etAccount.setText("");
        });
        ClickUtil.click(200, ivPwdToggle, () -> {
            if (isSee) {
                isSee = false;
                etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                ivPwdToggle.setImageResource(R.mipmap.app_icon_show);
            } else {
                isSee = true;
                etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                ivPwdToggle.setImageResource(R.mipmap.app_icon_hide);
            }
        });
        ClickUtil.click(tvPwdForget, () -> {
            ARouter.getInstance().build(RConfig.APP_PWD_FORGET).navigation();
        });

        inputs = new AndroidNextInputs();
        access = new WidgetAccess(this);

        inputsCheckCode = new AndroidNextInputs();

    }

    public void loginSuccess(LoginBean data) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.put(SPConfig.ACCOUNT, data.getAccount());
        httpHeaders.put(SPConfig.TOKEN, data.getToken());
        SPUtil.put(SPConfig.ACCOUNT, data.getAccount());
        SPUtil.put(SPConfig.TOKEN, data.getToken());
        SPUtil.put(SPConfig.MOBILE, etAccount.getText().toString());
        SPUtil.put(SPConfig.GOOGLETYPE, data.getIs_start_google());
        SPUtil.put(SPConfig.LOGIN, true);
        OkGo.getInstance().addCommonHeaders(new HttpHeaders());
        OkGo.getInstance().getCommonHeaders().clear();
        OkGo.getInstance().addCommonHeaders(httpHeaders);
        userViewModel.update();
        finish();
        overridePendingTransition(R.anim.lib_anim_out, R.anim.lib_anim_back);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateAccount(AccountBean accountBean) {
        if (etAccount != null) {
            etAccount.setText(accountBean.getAccount());
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.lib_anim_out, R.anim.lib_anim_back);

    }

    public void googleCheck(LoginBean loginBean) {
        if (googleCheckDialog == null) {
            googleCheckDialog = AppTipUtil.showGoogleCheckDialog(this, pwd -> {
                mPresenter.checkGoogle(pwd, loginBean);
            });
        } else {
            googleCheckDialog.show();
        }
    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(timeDis);
        RxBus.getDefault().unregister(this);

        super.onDestroy();
    }
}
